/////////////////////////////////////////////////////////////////////////////
// Defines interfaces for winpcap functions.
//
// Version 1.0 - 15 june 2007
//
// Copyright (c) 2007 by FlexByte Software (http://flexbyte.com)
// All rights reserved.
//
// You may use the source code in any product (freeware, commercial, etc.). 
// You may modify the source code as you want except 
// you cannot modify the copyright details at the top of each module.
// If you have a web site or blog give a link to our web site, flexbyte.com
/////////////////////////////////////////////////////////////////////////////
#ifndef adapter_h
#define adapter_h
#include <pcap.h>
#include <string>
#include "FlexThread.h"
#include "FlexException.h"
/////////////////////////////////////////////////////////////////////////////
struct Packet
{
	struct pcap_pkthdr *	_header;
	const u_char *			_data;
	int						_offset;
};
/////////////////////////////////////////////////////////////////////////////
struct IPacketAnalyzer
{
	virtual void AnalyzePacket(const Packet & packet)	= 0;
	virtual void AnalyzeError(const CFlexException & e)	= 0;
	virtual void OnAnalyzeStart() = 0;
	virtual void OnAnalyzeStop() = 0;
};
/////////////////////////////////////////////////////////////////////////////
// CAdapter
/////////////////////////////////////////////////////////////////////////////
class CAdapter 
	: public IExecutable
{
// Constructor/Destructor
public:
	CAdapter(IPacketAnalyzer * pAnalyzer);
	virtual ~CAdapter(void);

// Attributes
	pcap_t*		GetDevice() const;
	const char *		GetName() const;
	const char *		GetType() const;
	int					GetDataLink() const;
	bool				IsWorking() const;

// Implementation
	void				Open(const char * szDeviceName,
							int iBufferMax = INFINITE , /*not limit*/
							int iPacketSize = 0xFFFF,
							int iTimeout = 100,
							bool bPromisc = false
							);
	void				Close();
	void				SetFilter(char * szFilter, u_int netmask = 0xFFFFFFFF);

	// TODO: add other WinPcap functions here
protected:
	int					GetPacketOffset();
    void 				SetName();
// Override
	virtual void		Execute();
	virtual void		Break();

// Members
protected:
	std::string			m_sDeviceName;
	std::string			m_sDeviceType;
	bool				m_bWork;
	int 				m_iBufferMax;
	pcap_t *			m_hDevice;
	CFlexThread			m_thread;
	IPacketAnalyzer *	m_pAnalyzer;
};
inline pcap_t * CAdapter::GetDevice() const
{
	return m_hDevice;
}
/////////////////////////////////////////////////////////////////////////////
inline const char * CAdapter::GetName() const
{
	return m_sDeviceName.c_str();
}
/////////////////////////////////////////////////////////////////////////////
inline const char * CAdapter::GetType() const
{
	return m_sDeviceType.c_str();
}
/////////////////////////////////////////////////////////////////////////////
inline bool CAdapter::IsWorking() const
{
	return m_bWork;
}
/////////////////////////////////////////////////////////////////////////////
#endif
